package nccloud.web.pu.buyingreq.action;

import nc.vo.pu.m20.entity.PraybillHeaderVO;
import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillSupplierVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pu.m20.enumeration.EnumPraySource;
import nc.vo.pu.m20.enumeration.EnumRevise;
import nc.vo.pu.pub.enumeration.POEnumBillStatus;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCardOperator;
import nccloud.pubitf.pu.buyingreq.service.IBuyingReqQueryCardInfoService;
import nccloud.pubitf.scmpub.pub.util.BillSagaFieldsClearUtil;
import nccloud.web.pu.buyingreq.entity.QueryInfoBuyingreq;
import nccloud.web.pu.buyingreq.util.BuyingReqScaleUtil;
import nccloud.web.pu.buyingreq.util.PublicUtil;

public class BuyingreqCopyAction implements ICommonAction {
	public Object doAction(IRequest request) {
		String read = request.read();
		IJson json = JsonFactory.create();

		QueryInfoBuyingreq query = (QueryInfoBuyingreq) json.fromJson(read, QueryInfoBuyingreq.class);

		try {
			IBuyingReqQueryCardInfoService service = (IBuyingReqQueryCardInfoService) ServiceLocator
					.find(IBuyingReqQueryCardInfoService.class);

			PraybillVO[] vos = service.queryVOs(new String[] { query.getKeyword() });
			if (null == vos || vos.length == 0) {
				PublicUtil util = new PublicUtil();
				util.afterError();
			}
			PraybillVO vo = vos[0];

			setHeadValue(vo);

			setBodyValue(vo);

			BillSagaFieldsClearUtil.initAggregatedValueObject(new PraybillVO[] { vo });

			BillCardOperator operator = new BillCardOperator(query.getPageid());
			BillCard billCard = operator.toCard(vo);
			afterProcess(billCard);
			ExtBillCardOperator extOperator = new ExtBillCardOperator(query.getPageid());
			ExtBillCard extbillCard = extOperator.toCard(vo);
			return extbillCard;
		} catch (BusinessException ex) {
			ExceptionUtils.wrappBusinessException(ex.getMessage());

			return null;
		}
	}

	public void afterProcess(BillCard card) {
		BuyingReqScaleUtil buyUtil = new BuyingReqScaleUtil();
		buyUtil.processBillCard(card, false);
	}

	private void setHeadValue(PraybillVO billVO) {
		PraybillHeaderVO headerVO = billVO.getHVO();

		headerVO.setPk_praybill(null);

		headerVO.setPk_srcpraybill(null);

		headerVO.setBislatest(UFBoolean.TRUE);

		headerVO.setBshowLatest(UFBoolean.TRUE);

		headerVO.setModifyStatus(Integer.valueOf(EnumRevise.SIMPLE.toIntValue()));

		headerVO.setBdirecttransit(UFBoolean.FALSE);

		headerVO.setVbillcode(null);

		long time = SessionContext.getInstance().getClientInfo().getBizDateTime();
		UFDate date = new UFDate(time);
		headerVO.setDbilldate(date);

		headerVO.setFpraysource(EnumPraySource.MANUAL.toInteger());

		headerVO.setFbillstatus(POEnumBillStatus.FREE.toInteger());

		headerVO.setNversion(Integer.valueOf(1));

		headerVO.setIprintcount(null);

		headerVO.setTs(null);

		headerVO.setBillmaker(SessionContext.getInstance().getClientInfo().getUserid());

		headerVO.setCreationtime(null);

		headerVO.setModifier(null);

		headerVO.setModifiedtime(null);

		headerVO.setCreviseoperid(null);

		headerVO.setTrevisiontime(null);

		headerVO.setApprover(null);

		headerVO.setTaudittime(null);

		headerVO.setDmakedate(null);
		headerVO.setCextsrcsys(null);
	}

	private void setBodyValue(PraybillVO billVO) {
		PraybillItemVO[] itemVOs = billVO.getBVO();

		for (int i = 0; i < itemVOs.length; i++) {

			itemVOs[i].setPk_praybill_b(null);

			itemVOs[i].setPk_srcpraybillb(null);

			itemVOs[i].setPk_praybill(null);

			itemVOs[i].setNaccumulatenum(null);

			itemVOs[i].setNgenct(null);

			itemVOs[i].setNpriceauditbill(null);

			itemVOs[i].setNquotebill(null);

			itemVOs[i].setBrowclose(UFBoolean.FALSE);

			itemVOs[i].setBpublishtoec(UFBoolean.FALSE);

			itemVOs[i].setVsourcecode(null);

			itemVOs[i].setCsourceid(null);

			itemVOs[i].setCsourcebid(null);

			itemVOs[i].setCsourcetypecode(null);

			itemVOs[i].setVsrctrantypecode(null);

			itemVOs[i].setVsourcerowno(null);

			itemVOs[i].setCfirstid(null);

			itemVOs[i].setCfirstbid(null);

			itemVOs[i].setCfirsttypecode(null);

			itemVOs[i].setVfirstcode(null);

			itemVOs[i].setVfirstrowno(null);

			itemVOs[i].setVfirsttrantype(null);

			itemVOs[i].setTs(null);

			itemVOs[i].setBisgensaorder(UFBoolean.FALSE);

			itemVOs[i].setBisarrange(UFBoolean.FALSE);

			itemVOs[i].setDbilldate(billVO.getHVO().getDbilldate());

			itemVOs[i].setCextsrcid(null);
			itemVOs[i].setCextsrcbid(null);
			itemVOs[i].setCextsrcbilltype(null);
			itemVOs[i].setVextsrcbillcode(null);

			itemVOs[i].setBtoyyc(UFBoolean.FALSE);

			itemVOs[i].setVyycreason(null);
		}
		
		PraybillSupplierVO[] supplierVOs = billVO.getSupplierVO();

		for (int i = 0; i < supplierVOs.length; i++) {
			supplierVOs[i].setPk_praybill(null);
			supplierVOs[i].setPk_praybill_supplier(null);
		}
	}
}
